/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-16
 * Time: 14:22
 */
//class Person implements Cloneable {
//    private String name;
//
//    public Person clone() {
//        Person o = null;
//        try {
//            o = (Person) super.clone();
//        }catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//        }
//        return o;
//    }

//public class Test {
//    public static void main(String[] args) {
//        Person p1 = new Person();
//        Person p2 = p1.clone();
//        System.out.println(p1 == p2);
//    }
//}



class Person implements Cloneable {
    private String name;
    private Money money;

    public Money getMoney() {
        return money;
    }

    public void setMoney(Money money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // 实现克隆Person对象
    @Override
    protected Person clone() throws CloneNotSupportedException {
        money = this.money.clone();
        return (Person) super.clone();
    }
}

class Money implements Cloneable {
    private double m;


    // 实现克隆Money对象
    @Override
    protected Money clone() throws CloneNotSupportedException {
        return (Money) super.clone();
    }

    public double getMoney() {
        return m;
    }

    public void setMoney(double m) {
        this.m = m;
    }
}



public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person();
        p1.setName("球球");
        Money money = new Money();
        money.setMoney(99.0);
        p1.setMoney(money);
        Person p2 = p1.clone();
        System.out.println("通过p2修改前的结果");
        System.out.println(p1.getName());
        System.out.println(p2.getName());
        System.out.println(p1.getMoney().getMoney());
        System.out.println(p2.getMoney().getMoney());
        System.out.println("通过p2修改后的结果");
        p2.setName("优优");
        money.setMoney(12.2);
        p2.setMoney(money);
        System.out.println(p1.getName());
        System.out.println(p2.getName());
        System.out.println(p1.getMoney().getMoney());
        System.out.println(p2.getMoney().getMoney());
    }
}





